import {defineStore} from 'pinia';
import {getTypeList} from '@/api/api.js';
import {ref} from 'vue'

export const useTypeStore = defineStore('type', () => {
    const payType = ref({});
    const inType = ref({});

    // 用于首页汇总
    const selectTypeList = ref({});

    async function getData() {
        const list = (await getTypeList()).list;
        payType.value = list.filter(item => item.type === 0)
        inType.value = list.filter(item => item.type === 1)


        let payArr = list.filter(item => item.type === 0).map(item => {
            return {
                childKey: item.title,
                value: item.id
            };
        })

        let inArr = list.filter(item => item.type === 1).map(item => {
            return {
                childKey: item.title,
                value: item.id
            };
        })

        let selectTypeArr=[]
        selectTypeArr.push({key: "支出", values: payArr});
        selectTypeArr.push({key: "收入", values: inArr});
        selectTypeList.value = selectTypeArr
    }

    return {payType, inType,selectTypeList, getData};
});